<?php
/**
 * Description
 *
 * PHP version 7
 *
 * @category Custom
 * @package  Custom
 * @author   ryu <mo5467@126.com>
 * @date     2020-04-30 00:03:06
 * @lastTime 2020-05-01 23:16:07
 * @license  https://opensource.org/licenses/MIT MIT License
 * @link     none
 */
namespace Design\Other\Repository;

class InMemoryPersistence implements Persistence
{

    private $data = [];

    private $lastId = 0;


    public function generateId() : int
    {
        $this->lastId++;
        return $this->lastId;

    }//end generateId()


    public function persist(array $data)
    {
        $this->data[$this->lastId] = $data;

    }//end persist()


    public function retrieve(int $id) : array
    {
        if (!isset($this->data[$id])) {
            throw new \OutOfBoundsException(sprintf('No data found for id %d', $id));
        }

        return $this->data[$id];

    }//end retrieve()


    public function delete(int $id)
    {
        if (!isset($this->data[$id])) {
            throw new \OutOfBoundsException(sprintf('No data found for id %d', $id));
        }

        unset($this->data[$id]);

    }//end delete()


}//end class
